{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# @class method, @static method, @property method",
   "id": "d2944a51e48430c6"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## @方法.setter\n",
    "## @方法.getter"
   ],
   "id": "47a4fd9b05c6b717"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-30T08:07:59.416137Z",
     "start_time": "2024-10-30T08:07:59.412481Z"
    }
   },
   "cell_type": "code",
   "source": [
    "class Person:\n",
    "    def __init__(self, name, age):\n",
    "        self.name = name\n",
    "        self.__age = age\n",
    "        \n",
    "    @property\n",
    "    def age(self):\n",
    "        return self.__age\n",
    "    \n",
    "    # 类似拦截器\n",
    "    # 给设置私有属性的方法加一个判断，数据是否合法等\n",
    "    @age.setter\n",
    "    def age(self, age):\n",
    "        if not isinstance(age, int):\n",
    "            print(\"age is not an integer\")\n",
    "            return\n",
    "        elif age < 18 or age > 100:\n",
    "            print(\"age is not between 18 and 100\")\n",
    "            return\n",
    "        else:\n",
    "            self.__age = age\n",
    "    \n",
    "    # 用户使用这个属性时，先进行判断，不符合条件，不让用户获取该属性\n",
    "    @age.getter\n",
    "    def age(self):\n",
    "        if self.__age == 0:\n",
    "            print(\"age is zero\")\n",
    "        else:\n",
    "            return self.__age\n",
    "        \n",
    "        \n",
    "        \n",
    "p1 = Person(\"John\", 0)\n",
    "print(p1.age)\n",
    "p1.age = 10\n",
    "print(p1.age)"
   ],
   "id": "f70872f6d5ccc34d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "age is zero\n",
      "None\n",
      "age is not between 18 and 100\n",
      "age is zero\n",
      "None\n"
     ]
    }
   ],
   "execution_count": 9
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
